home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group99a.txt
/
000220_icon-group-sender _Thu Oct 21 15:50:06 1999.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
3KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id PAA05811
for icon-group-addresses; Thu, 21 Oct 1999 15:49:56 -0700 (MST)
Message-Id: <199910212249.PAA05811@baskerville.CS.Arizona.EDU>
From: gep2@terabites.com
Date: Thu, 21 Oct 1999 13:52:24 -0500
Subject: List subscription insert/removal
To: icon-group@optima.CS.Arizona.EDU
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
> I am trying to figure out how to insert a new element into a list,
and remove an element from the middle of a list (not the end).
In answer to this question previously Gordon Peterson sent
a reply. I tried what he said and it doesn't work.
It does if you do it right. :-)
> My original question, that he answered, was
"I find no routines to pluck one element out of the middle of a list, or insert
one in. Are there such routines?".
> His answer was:
"Perhaps not, since those functions are really pretty trivial (you can use
subscript ranges to specify sublists, making that kind of thing a simple
expression).".
> I assume he meant that I can insert an element into a list the same way
I can insert a character into a string variable. So I tried it, 4 ways; none
of them worked, they all produce a run-time error and abort. Here is
the program. Though I use mere charactes or 1-char strings as list
elements, it is just to try out list manipulation...
> procedure main()
local s # it will be a list
> s:= ["a", "b", "c", "d"]
WriteL(s) # this writes "abcd" as it should
# Next, I want to insert "x" as a list element between "b" and "c" ....
# didn't work: s[3:3]:="x"
# didn't work: s[3:3] := ["x"]
# didn't work: s[3+:0] := ["x"]
s[3+:0] := "x" # didn't work; I get a run-time error and program abort
WriteL(s)
end
> procedure WriteL(L)
local i, len
len := *L
i := 1
while i <= len do { writes(L[i]) ; i +:= 1 }
write()
end
> I get the following error message when I run the above:
Run-time error 111
File E:\icon\wsource\sublist.icn; Line 28 [the line: s[3+:0] := "x" ]
variable expected
offending value: list_3 = []
Traceback:
main()
{list_3 = [] := "x"} from line 28 in E:\icon\wsource\sublist.icn
> Can you tell me how to accomplish what I want?
Try sectioning the list and concatenating (if you have the Icon book, 1st
edition, this is on pages 52 and 53). In particular, for your test example
above, the code might look like:
s := s[1:2] ||| ["x"] ||| s[3:0]
The book states (page 53, but anyhow in the description of List Sections): "In
particular, a[i:j] is a list that is distinct from 'a', and assignment cannot be
made to it to change 'a'."
If you don't have a copy of the Icon book, you *really* ought to get a copy. It
really is VERY important if you plan to make effective use of the language, and
will pretty instantly answer most of this kind of question.
Gordon Peterson
http://web2.airmail.net/gep2/
Support the Anti-SPAM Amendment! Join at http://www.cauce.org/
12/19/98: the day the Conservatives demonstrated their scorn for their
fraudulent sham of representative government. Voters, remember it!